<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@taglib prefix="mvc" uri="http://www.springframework.org/tags/form" %>
<%@taglib prefix="fmt" uri="http://www.springframework.org/tags" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>项目立项</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=10,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0">
    <link rel="stylesheet" type="text/css" href="/lib/layui/layui/css/layui.css"/>
    <script type="text/javascript" src="/js/news/jquery-1.9.1.js"></script>
    <script src="/js/base/base.js" type="text/javascript" charset="utf-8"></script>
    <script src="../../js/jquery/jquery.cookie.js"></script>
    <script src="/lib/jQuery-File-Upload-master/jquery.ui.widget.js" type="text/javascript" charset="utf-8"></script>
    <script src="/lib/jQuery-File-Upload-master/jquery.iframe-transport.js" type="text/javascript"
            charset="utf-8"></script>
    <script src="/lib/jQuery-File-Upload-master/jquery.fileupload.js" type="text/javascript" charset="utf-8"></script>
    <script type="text/javascript" src="/js/common/fileupload.js"></script>
    <script type="text/javascript" src="/lib/layui/layui/layui.js"></script>
    <script type="text/javascript" src="/lib/layui/layui/layui.all.js"></script>
</head>
<style>
    .mbox {
        padding: 8px
    }

    .item img {
        height: 40px;
    }

    .btn {
        height: 30px;
    }

    .btn button {
        float: right;
    }

    .information {
        background: #e8f4fc;
        height: 30px;
        line-height: 30px;
    }
    .layui-form-label{
        width: 77px;
    }
    .openFile input[type=file]{
        position: absolute;
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
        width: 100%;
        height: 18px;
        z-index: 99;
        opacity: 0;
    }
    .layui-layer-content{
        line-height: 70px;
        text-align: center;
    }
    .typeIpt {
        line-height: 30px;
        margin-left: 10px;
    }
    .layui-icon-ok{
        margin-top: 6px;
    }
</style>
<body>
<div class="mbox">
    <div class="item">
        <img src="../img/yeji.png" alt="" style="margin: 0 5px 0 20px;"> <span
            style="font-size: 22px;display: inline-block;vertical-align: middle;">项目立项</span>
    </div>
    <hr class="layui-bg-blue">
    <%--    <div class="btn" style="display: inline-flex;">--%>
    <%--        <span class="typeIpt layui-form">--%>
    <%--            <input type="radio" checked class="approve" lay-filter="status" value="1" name="status" title="已立项"/>&nbsp;--%>
    <%--            <input type="radio" lay-filter="status" value="2" name="status" title="待立项"/>&nbsp;--%>
    <%--        </span>--%>
    <%--    </div>--%>
    <div class="btn">
        <button type="button" class="layui-btn layui-btn-sm" style="background: #2b7fe0"><i
                class="layui-icon layui-icon-upload-drag" style="background: #2b7fe0"></i>导出Excel
        </button>
        <button type="button" class="layui-btn layui-btn-sm"  style="background: #2b7fe0;margin-right: 20px"><i
                class="layui-icon layui-icon-addition" style="vertical-align: inherit"></i>查询
        </button>
        <button type="button" class="layui-btn layui-btn-sm " id="subBtn"  style="background: #2b7fe0;margin-right: 20px" onclick="plCreat()"><i
                class="layui-icon layui-icon-addition" style="vertical-align: inherit" ></i>批量立项
        </button>
    </div>
    <table class="layui-hide" id="test" lay-filter="test"></table>
    <input type="text" id="date1" style="display: none">
</div>
</body>
</html>
<script type="text/html" id="barDemo">
    {{# if(  d.remarks == undefined || d.remarks.length == 0  || ( d.remarks.map(item=>{return ( item.trType == '1' && item.trStatus == '1') }).indexOf(true) > -1 ) ){  }}
    <a class="layui-btn layui-btn-xs" lay-event="score">立项</a>
    {{#  } }}
</script>
<script>
    var denglu = '';
    var table,form,laydate,upload;

    $(function(){
        //获取当前登陆人
        $.ajax({
            url: "/getLoginUser",
            type: 'post',
            dataType: 'json',
            success: function (res) {
                denglu = res.object.deptName
            }
        })

        layui.use(['table', 'form', 'laydate','upload'], function () {
            table = layui.table,
                form = layui.form,
                laydate = layui.laydate,
                upload = layui.upload;

            table.render({
                elem: '#test'
                ,url:'/Recognize/getProject'
                , title: '用户数据表'
                , page: false
                , cols: [[
                    {type: 'checkbox', fixed: 'left'}
                    , {field: 'topicName', title: '项目名称'}
                    , {field: 'planName', title: '申报计划名称'}
                    , {title: '项目成员',templet:function(d){
                            var rpmSignatures = d.rpmSignatures;
                            var nameId = '';
                            for(var i=0;i<rpmSignatures.length;i++){
                                if(rpmSignatures[i].userName != ''){
                                    nameId += rpmSignatures[i].userName +',';
                                }
                            }
                            return nameId
                        }}
                    , {field: 'userName', title: '负责人'}
                    , { title: '专家1 评分',templet:function(d){
                            var scoreList = d.scoreList;
                            if (scoreList != undefined && scoreList.length > 0 && scoreList[0] != undefined){
                                return scoreList[0].rsScore;
                            }
                            return '';
                        }}
                    , { title: '专家1 意见',templet:function(d){
                            var scoreList = d.scoreList;
                            if (scoreList != undefined && scoreList.length > 0 && scoreList[0] != undefined){
                                return scoreList[0].remarks;
                            }
                            return '';
                        }}
                    , { title: '专家2 评分',templet:function(d){
                            var scoreList = d.scoreList;
                            if (scoreList != undefined && scoreList.length > 0 && scoreList[1] != undefined){
                                return scoreList[1].rsScore;
                            }
                            return '';
                        }}
                    , { title: '专家2 意见',templet:function(d){
                            var scoreList = d.scoreList;
                            if (scoreList != undefined && scoreList.length > 0 && scoreList[1] != undefined){
                                return scoreList[1].remarks;
                            }
                            return '';
                        }}
                    , { title: '专家3 评分',templet:function(d){
                            var scoreList = d.scoreList;
                            if (scoreList != undefined && scoreList.length > 0 && scoreList[2] != undefined){
                                return scoreList[2].rsScore;
                            }
                            return '';
                        }}
                    , { title: '专家3 意见',templet:function(d){
                            var scoreList = d.scoreList;
                            if (scoreList != undefined && scoreList.length > 0 && scoreList[2] != undefined){
                                return scoreList[2].remarks;
                            }
                            return '';
                        }}
                    , { title: '专家4 评分',templet:function(d){
                            var scoreList = d.scoreList;
                            if (scoreList != undefined && scoreList.length > 0 && scoreList[3] != undefined){
                                return scoreList[3].rsScore;
                            }
                            return '';
                        }}
                    , { title: '专家4 意见',templet:function(d){
                            var scoreList = d.scoreList;
                            if (scoreList != undefined && scoreList.length > 0 && scoreList[3] != undefined){
                                return scoreList[3].remarks;
                            }
                            return '';
                        }}
                    , {field: 'avg', title: '平均分'}
                    , { title: '操作', toolbar: '#barDemo'}
                ]]
                ,parseData:function(res){
                    return {
                        "code":0, //解析接口状态
                        "data": res.object //解析数据列表
                        , page: false
                    };

                }
            });

            //监听行工具事件
            table.on('tool(test)', function(obj){
                var data = obj.data;
                var topicID = data.topicId //项目
                var trPlanId = data.planId;  //计划
                var remarks = data.remarks; //扩展表数据

                if(obj.event === 'score') {
                    layer.open({
                        type: 1,
                        title: '基本信息',
                        btn: ['确定', '取消'],
                        shade: 0.5,
                        area: ['40%', '60%'],
                        content: '<div id="cont" style="margin: 10px">\n' +
                            '        <form class="layui-form" action="" id="ajaxforms" lay-filter="ajaxforms">\n' +
                            '            <h3 class="information">【基本信息】</h3>\n' +
                            '            <div class="layui-form-item" style="margin-top: 15px">\n' +
                            '                <div class="layui-inline">\n' +
                            '                   <span style="color:red">*</span><label class="layui-form-label">项目名称:</label>\n' +
                            '                    <div class="layui-input-inline">\n' +
                            '                        <input disabled type="text" name="planName" lay-verify="required|phone" autocomplete="off"\n' +
                            '                               class="layui-input required layui-disabled" value="'+data.planName+'">\n' +
                            '                    </div>\n' +
                            '                </div>\n' +
                            '            </div>\n' +
                            '            <div class="layui-form-item" style="margin-top: 15px">\n' +
                            '                <div class="layui-inline">\n' +
                            '                   <span style="color:red">*</span><label class="layui-form-label">立项编号:</label>\n' +
                            '                    <div class="layui-input-inline">\n' +
                            '                        <input disabled type="text" name="planNameBian" lay-verify="required|phone" autocomplete="off"\n' +
                            '                               class="layui-input required layui-disabled">\n' +
                            '                    </div>\n' +
                            '                </div>\n' +
                            '            </div>\n' +
                            '            <div class="layui-form-item">\n' +
                            '                <div class="layui-inline">\n' +
                            '                    <span style="color:red">*</span><label class="layui-form-label">立项日期：</label>\n' +
                            '                    <div class="layui-input-inline">\n' +
                            '                        <input type="text" name="beginDate" id="date" lay-verify="date"\n' +
                            '                               autocomplete="off" class="layui-input required">\n' +
                            '                    </div>\n' +
                            '                </div>\n' +
                            '            </div>\n' +
                            '            <div class="layui-form-item">\n' +
                            '                <div class="layui-inline">\n' +
                            '                    <span style="color:red">*</span><label class="layui-form-label" style="padding: 0;margin: 0;width: 101px;position: relative;top: 7px;left: -7px;">批准单位：</label>\n' +
                            '                    <div class="layui-input-inline">\n' +
                            '                        <input disabled type="text" name="endDateDan"  lay-verify="date" autocomplete="off" class="layui-input required layui-disabled" style="margin-left: 6px;">\n' +
                            '                    </div>\n' +
                            '                </div>\n' +
                            '            </div>\n' +

                            ' <div class="layui-form-item">\n' +
                            '    <div class="layui-inline">\n' +
                            '      <label class="layui-form-label">上传附件</label>\n' +
                            '      <div class="layui-input-inline" style="width: 150px">\n' +
                            '        <ul id="fileul"></ul>' +
                            '        <button type="button" class="layui-btn layui-btn-sm" id="test1">\n' +
                            '          <i class="layui-icon">&#xe67c;</i>上传附件\n' +
                            '       </button>\n' +
                            '      </div>\n' +
                            '    </div>\n' +
                            '  </div>' +
                            '        </form>\n' +
                            '    </div>',
                        success: function () {
                            $('input[name="endDateDan"]').val('普陀区教育局');

                            //立项编号
                            $.ajax({
                                url: "/Recognize/getProjectNo",
                                type: 'post',
                                dataType: 'json',
                                data:{topicId:topicID},
                                success: function (res) {
                                    $('input[name="planNameBian"]').val(res.object)
                                }
                            })


                            //附件上传
                            form.render();
                            fileuploadFn('#fileupload', $('#fileAll'));
                            upload.render({
                                elem: '#test1'
                                ,url: '/upload'
                                ,data:{
                                    module:'remark'
                                }
                                ,done: function(res, index, upload){
                                    if (res.flag){
                                        var obj = res.obj;
                                        for (var i = 0; i < obj.length; i++) {
                                            var str = '<li>\n' +
                                                '                    <a href="/download?' + obj[i].attUrl + '" name="' + obj[i].attachName + '" attachId="' + obj[i].attachId + '" style="text-decoration:none;margin-left:5px;">\n' +
                                                '                        <img src="/img/attachment_icon.png">\n' +
                                                obj[i].attachName +
                                                '                    </a>\n' +
                                                '                    <img class="deImgs" style="margin-left:5px;cursor: pointer;" src="/img/file/icon_deletecha_03.png">\n' +
                                                '                </li>';
                                            $('#fileul').append(str);
                                        }
                                    }else{
                                        layer.msg('上传失败');
                                    }
                                }
                                ,accept: 'file'
                            })


                            form.val('ajaxforms', data);
                            //默认显示当前时间
                            laydate.render({
                                elem: '#date',
                                value: new Date()
                            });
                        },
                        yes:function (index, layero) {

                            //处理附件
                            var trAttachmentName = ''
                            var trAttachmentId = ''
                            $("#fileul li").each(function(index){
                                trAttachmentName += $(this).find('a').attr('name')+ '*'
                                trAttachmentId += $(this).find('a').attr('attachId') +','
                            });

                            var topicJson = []
                            var json = {}
                            if ( remarks != undefined && remarks.length > 0 ){
                                var remark =  remarksIndex(remarks);
                                json['trId'] = remark.trId;
                                //如果传trId就会做修改的操作
                            }
                            json.trTopicId = topicID  //项目
                            json.trPlanId =trPlanId
                            json.trType = 1
                            json.trStatus = 2
                            json.trTime = $('input[name="beginDate"]').val()
                            json.trOther = $('input[name="endDateDan"]').val()
                            json.trAttachmentName = trAttachmentName
                            json.trAttachmentId = trAttachmentId
                            topicJson.push(json)

                            $.ajax({
                                url: "/Recognize/updateProject",
                                type: 'post',
                                dataType: 'json',
                                data:{topicJson:JSON.stringify(topicJson)},
                                success: function (res) {

                                    // tableData.reload();
                                    window.location.reload();
                                }
                            })
                            layer.close(index);
                        }
                    })
                }
            });

            form.render();
        });

    })

    //获取立项后的数据
    function remarksIndex(remarks) {
        for (var i = 0; i < remarks.length; i++) {
            if (remarks[i].trType === '1' ){
                return remarks[i];
            }
        }
    }

    //批量立项
    function plCreat() {
        var chooseArr = layui.table.checkStatus('test').data;
        var topicJson = []
        laydate.render({
            elem: '#date1',
            format: 'yyyy-MM-dd HH:mm:ss', //指定时间格式
            value: new Date(),
        });
        if(chooseArr.length == 0){
            layer.alert('请至少选择一条任务！', {icon: 7});
            return  false;
        }
        for(var i=0;i<chooseArr.length;i++){
            var remarks = chooseArr[i].remarks;

            //判断选中的是否包含已经立项的
            var isPro = remarks.map( item => { return ( item.trType === '1' && item.trStatus === '2'  ) } );
            if ( isPro.indexOf(true) > -1 ){
                // layer.msg('你选中的数据包含已经立项过的');
                layer.alert('你选中的数据包含已经立项过的！', {icon: 7})
                return  false;
            }

            //封装数据
            var json = new  Object();
            if ( remarks != undefined && remarks.length > 0 ){
                var remark =  remarksIndex(remarks);
                json['trId'] = remark.trId;
            }
            json.trTopicId = chooseArr[i].topicId
            json.trPlanId = chooseArr[i].planId
            json.trType = 1
            json.trStatus = 2
            json.trTime = $('#date1').val()
            json.trOther = denglu
            topicJson.push(json)
        }

        // return false;
        layer.confirm('是否批量立项?', {icon: 3, title:'提示'}, function(index){
            $.ajax({
                url: '/Recognize/updateProject',
                type: 'post',
                data:{topicJson:JSON.stringify(topicJson)},
                success: function () {
                    table.reload('test');
                    layer.close(index);
                }
            })
        });


    }

</script>
<script>

</script>